package com.example.testi18n.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author xiaobendan
 * @version v1.0.0
 * @className BigDecimalSerialize
 * @package com.pig4cloud.pigx.edaominer.config
 * @description
 * @date 2022-08-16 11:43
 */
public class BigDecimalSerialize extends JsonSerializer<BigDecimal> {


    @Override
    public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(Objects.nonNull(value)) {
            //返回到前端的数据为字符串类型
            jsonGenerator.writeString(value.stripTrailingZeros().toPlainString());
        }else {//如果为null的话，就写null
            jsonGenerator.writeString("0.00");
        }
    }
}
